package com.lvmama.task.job;

import com.lvmama.task.common.utils.TaskUtil;
import com.lvmama.task.job.common.ContextFactory;
import com.lvmama.task.job.common.TaskJobParameters;
import com.lvmama.task.job.monitor.TaskMonitor;
import org.apache.commons.cli.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.management.ManagementFactory;

/**
 * Created by troy on 16/6/2.
 * kouhongyu@163.com
 */
public class Run {

    private static Log LOG = LogFactory.getLog(Run.class);

    public static void main(String[] args) {

        /**
         * 初始化命令行参数
         */
        initCommandLine(args);

        /**
         * Spring 加载
         */
        ContextFactory.getInstance().putContext("applicationContext.xml");

        /**
         * Monitor 启动
         */
        new Thread(new TaskMonitor(TaskJobParameters.TASK_MONITOR_PORT)).start();

        /**
         * 打印启动信息
         */
        printStartInfo();
    }

    private static void initCommandLine(String[] args) {

        CommandLineParser parser = new BasicParser();

        Option hOpt = new Option("h", "help", false, "Print this usage information");

        Option nOpt = new Option("n", "node", true, "set job node\nexample : -n node_A");
        nOpt.setArgName("String");

        Option oOpt = new Option("o", "owner", true, "set job owner\nDetermine the environment\nexample : -o thursday");
        oOpt.setArgName("String");

        Option pOpt = new Option("p", "port", true, "set monitor port\nexample : -p 22222");
        pOpt.setArgName("Number");

        Options options = new Options();
        options.addOption(hOpt);
        options.addOption(nOpt);
        options.addOption(oOpt);
        options.addOption(pOpt);


        try {
            CommandLine commandLine = parser.parse(options, args);

            if (commandLine.hasOption('h')) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("task-job", null, options, null, true);
                System.exit(0);
            }
            if (commandLine.hasOption('n')) {
                TaskJobParameters.TASK_JOB_NODE = commandLine.getOptionValue('n');
            }
            if (commandLine.hasOption('p')) {
                TaskJobParameters.TASK_MONITOR_PORT = commandLine.getOptionValue('p');
            }
            if (commandLine.hasOption('o')) {
                TaskJobParameters.TASK_JOB_OWNER = commandLine.getOptionValue('o');
            }

            /**
             * 如果命令行参数和配置文件都没有设置节点名,那么取机器名
             */
            if (StringUtils.isBlank(TaskJobParameters.TASK_JOB_NODE)) {
                TaskJobParameters.TASK_JOB_NODE = TaskUtil.hostName();
            }
            /**
             * 如果命令行参数和配置文件都没有设置监控端口,那设置为27777
             */
            if (StringUtils.isBlank(TaskJobParameters.TASK_MONITOR_PORT)) {
                TaskJobParameters.TASK_MONITOR_PORT = "27777";
            }
            /**
             * 如果命令行参数和配置文件都没有设置环境标识,那设置为online
             */
            if (StringUtils.isBlank(TaskJobParameters.TASK_JOB_OWNER)) {
                LOG.error("TASK_JOB_OWNER is null !!!");
                System.exit(0);
            }
            /**
             * 节点标识增加进程ID,避免同一节点标识被重复启动
             */
            TaskJobParameters.TASK_JOB_NODE += "#" + ManagementFactory.getRuntimeMXBean().getName();

        } catch (ParseException e) {
            LOG.error(e);
        }
    }

    private static void printStartInfo() {
        System.out.println("############################################");
        System.out.println("##");
        System.out.println("##        task-job Startting...");
        System.out.println("##");
        System.out.println("##           Job  node : " + TaskJobParameters.TASK_JOB_NODE);
        System.out.println("##           Job owner : " + TaskJobParameters.TASK_JOB_OWNER);
        System.out.println("##       Monitor  port : " + TaskJobParameters.TASK_MONITOR_PORT);
        System.out.println("##");
        System.out.println("############################################");
    }
}
